package com.karson.front.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.model.AlbumInfo;
import com.karson.common.model.UserFavorite;
import com.karson.common.model.UserFavoriteRecord;
import com.karson.common.request.FavoriteRecordRequest;
import com.karson.common.request.PageParamRequest;
import com.karson.common.request.UserFavoriteFolderRequest;
import com.karson.common.response.AlbumResponse;
import com.karson.common.response.CommonResult;
import com.karson.common.response.FavoriteAlbumResponse;
import com.karson.common.response.UserFavoriteFolderResponse;
import com.karson.common.vo.PageVo;
import com.karson.service.service.UserFavoriteRecordService;
import com.karson.service.service.UserFavoriteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/favorite")
@Tag(name = "收藏管理接口")
public class FavoriteController {
    @Autowired
    private UserFavoriteService userFavoriteService;

    @Autowired
    private UserFavoriteRecordService userFavoriteRecordService;
    @Operation(summary = "获取收藏夹列表")
    @GetMapping("/getFavFolder")
    public CommonResult<List<UserFavoriteFolderResponse>> getFavFolder(@RequestParam(required = false) Integer messageId) {
        List<UserFavoriteFolderResponse> responseList =  userFavoriteService.getFavFolder(messageId);
        return CommonResult.success(responseList);
    }
    @Operation(summary = "添加收藏夹")
    @PostMapping("/createFolder")
    public CommonResult<UserFavoriteFolderResponse> createFolder(@RequestBody UserFavoriteFolderRequest request) {
        UserFavoriteFolderResponse userFavoriteFolderResponse = userFavoriteService.createFolder(request);
        return CommonResult.success(userFavoriteFolderResponse);
    }
    @Operation(summary = "修改收藏夹")
    @PostMapping("/updateFolder")
    public CommonResult<String> updateFolder(@RequestBody UserFavoriteFolderRequest request) {
        Boolean flag = userFavoriteService.updateFolder(request);
        return flag ? CommonResult.success() : CommonResult.failed();
    }
    @Operation(summary = "添加收藏")
    @PostMapping("/addFavorite")
    public CommonResult<String> addFavorite(@RequestBody FavoriteRecordRequest request) {
        if (request.getMessageId() == null || request.getMessageId() <= 0 || request.getFolderIds() == null) {
            return CommonResult.failed();
        }
        userFavoriteService.addFavorite(request);
        return CommonResult.success();
    }
    @Operation(summary = "查询收藏夹内容")
    @GetMapping("/getFavorite/{folderId}")
    public CommonResult<PageVo<FavoriteAlbumResponse>> getFavorite(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest, @PathVariable Integer folderId) {
        PageVo<FavoriteAlbumResponse> albumResponses = userFavoriteService.getFavorite(pageParamRequest,folderId);
        return CommonResult.success(albumResponses);
    }
    @Operation(summary = "查询已经收藏过的收藏夹")
    @GetMapping("/getFavoriteFolderIds/{messageId}")
    public CommonResult<Map<String, List<Integer>>> getFavoriteFolderIds(@PathVariable Integer messageId) {
        List<Integer> favoriteFolder = userFavoriteService.getFavoriteFolder(messageId);
        Map<String, List<Integer>> result = new HashMap<>();
        result.put("favoritedFolderIds", favoriteFolder);
        return CommonResult.success(result);
    }
    @Operation(summary = "删除收藏内容")
    @DeleteMapping("/delFavorite")
    public CommonResult<String> delFavorite(@RequestBody List<Integer> unFavoriteIds,
                                            @RequestParam(required = false) Integer folderId) {
        userFavoriteRecordService.updateByIdBatch(unFavoriteIds,folderId);
        return CommonResult.success();
    }

}
